package com.haiyou.sdk.thirdSdk;

import com.alibaba.fastjson2.JSONObject;
import com.haiyou.common.system.Properties;
import com.haiyou.common.util.http.HttpUtils;
import com.haiyou.common.util.string.StringFormatUitls;
import com.haiyou.protocol.http.sdk.login.AuthRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 微信SDK
 * 
 * @author xingyuan
 * @date 2023年12月6日 上午9:32:09
 */
@Slf4j
@Component
public class KuaiShouSdkService {

	public static final String user_auth_format = "{}?app_id={}&app_secret={}&js_code={}&grant_type=authorization_code";
	/**
	 * 正常
	 */
	public static final int code_200 = 200;

	
	/**
	 * 登录验证
	 * @param request
	 * @return uid 
	 */
	public static String authSign(AuthRequest request) {
		//https://open.kuaishou.com/miniGameDocs/gameDev/open-function/login.html
		String baseUrl = Properties.sdk_kuaishou_user_auth_url();
		String code = request.getUid();
		if (code.equals("")) {
			// 返回错误码
			return "";
		}
		try {
			String appId = Properties.sdk_kuaishou_app_id();
			String secret = Properties.sdk_kuaishou_secret();
			String url = StringFormatUitls.format(user_auth_format, baseUrl, appId, secret, code);
			log.info("get url:{}", url);
			String wxResponse = HttpUtils.getThrowException(url);
			log.info("ks response:{}", wxResponse);
			JSONObject json = JSONObject.parseObject(wxResponse);
			json.remove("session_key");
			String openid = "open_id";
			if (json.containsKey(openid)) {
				String roleId = json.getString(openid);
				return roleId;
			} else {
				return "";
			}
		} catch (Exception e) {
			log.error("", e);
			return "";
		}

	}
	
	public static void main(String[] args) {
		AuthRequest x=  new AuthRequest();
		x.setUid("c201bdcb10484c7386d1ded57040bf46");
		//obbzz64mSX4iyzb3_FcY2AJzWYbA
		authSign(x);
		//https://api-sdk.essmicrogames.com/user/auth?account=2020092315165768
		//&app_id=ovdqy3we3nekjrz8&time=1600845363
		//&token=NwVe8n4zMizM1Mzg3MzZfMTYwMDg0NTM2M184MDkz
		//&sign=0aea0c2c60bb6988f83d8a8023906af5
				
		 
	}
}
